const File = require('../file/File');

class PrintOrder {
    constructor(obj) {
        this.type = obj.type;
        this.address = obj.address;
        this.name = obj.name;
        this.phone = obj.phone;
        this.count = obj.count;
        this.file = obj.file;
        this.fileName = obj.fileName;
        this.remark = obj.remark;
    }

    check() {
        // 验证 type
        if (!this.type) {
            return { success: false, err: "服务服务类型不能为空" };
        }
        if (['上门取件', '派送服务'].includes(this.type) == -1) {
            return { success: false, err: "服务类型只能是 \"上门取件\" 或者 \"派送服务\"" };
        }
        // 验证 address
        if (this.type == '派送服务' && !this.address) {
            return { success: false, err: "配送地址不能为空" };
        }
        if (this.address.length > 200) {
            return { success: false, err: "配送地址不能超过200个字符" };
        }
        // 验证 收件人姓名 name
        if (!this.name) {
            return { success: false, err: "收件人姓名不能为空" };
        }
        if (this.name.length > 50) {
            return { success: false, err: "收件人姓名不能超过50个字符" };
        }
        // 验证 联系电话 phone
        if (!this.phone) {
            return { success: false, err: "联系电话不能为空" };
        }
        if (this.phone.length > 20) {
            return { success: false, err: "联系电话不能超过20个字符" };
        }
        // 验证 打印份数 count
        if (!this.count) {
            return { success: false, err: "打印份数不能为空" };
        }
        // 验证 备注 remark
        if (this.remark.length > 5000) {
            return { success: false, err: "备注不能超过5000个字符" };
        }
        // 验证 文件
        this.file = new File({ file: this.file, fileName: this.fileName });
        let flag = this.file.check();
        if (!flag.success) {
            return flag;
        }
        return { success: true };
    }
}

module.exports = PrintOrder;